package com.b.controller;

import com.b.domain.ProductImage;
import com.b.service.ProductImageService;
import com.b.service.impl.ProductImageServiceImpl;
import com.b.utils.MinioUtils;
import com.b.vo.Result;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 商品图片表(product_image)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/product_image")
public class ProductImageController {
    /**
     * 服务对象
     */
    @Resource
    private ProductImageService productImageService;
    @Resource
    private MinioUtils minioUtils;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ProductImage selectOne(Integer id) {
        return productImageService.selectByPrimaryKey(id);
    }
    //根据商品Id查询商品图片
    @GetMapping("selectByProductId")
    public List<ProductImage> selectByProductId(@RequestParam("productId") Integer productId) {
        return productImageService.selectByProductId(productId);
    }
    //删除图片
    @PostMapping("/delete/{imageId}")
    public Result delete(@PathVariable("imageId") Integer imageId) {
        return productImageService.deleteByPrimaryKey(imageId) ? new Result().setCode("200").setMsg("删除成功") : new Result().setCode("400").setMsg("删除失败");
    }
    //添加图片
    @PostMapping("/add")
    public Result add( ProductImage productImage, @RequestParam("file") MultipartFile file) {
        if (file != null && !file.isEmpty()) {
            String url = minioUtils.upload(file);
            productImage.setImagePath(url);
        }
        return productImageService.insert(productImage) ? new Result().setCode("200").setMsg("添加成功") : new Result().setCode("400").setMsg("添加失败");
    }


}
